package com.htt.app.biancunghoangdao.clock;

import com.htt.app.biancunghoangdao.BiAnCungHoangDao;
import com.htt.app.biancunghoangdao.R;
import com.htt.app.biancunghoangdao.utils.Utils;
import com.htt.app.biancunghoangdao.view.PlayerView;

import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class Clock {
    /**
     * the handler that works instead of timer and supports UI
     */
    private Handler handler = new Handler();
    /**
     * the interval of the time refreshing
     */
    private long refreshStep = 0;
    
    /*
     * check clock is runing 
     */
    public boolean isRuning = false; 
    public static boolean isFinish = false;
    /**
     * pairs TextView timer+time/date format
     * icon clock
     */
    public TextView clockFace;
    public ImageButton icon;
    
    public Clock(TextView clockFace, ImageButton icon){
        this.clockFace=clockFace;
        this.icon = icon;
    }
    /**
     * fills all timer fields by actual time value, according to their formats.
     */
    private Runnable mUpdateTimeTask = new Runnable() {
       public void run() {
           showActualTimeDate(refreshStep);
           refreshStep = refreshStep - 1000;
           if(refreshStep >= 1000)
        	   handler.postDelayed(this,1000);
           else {
        	   finish();
           }
       }
    };

    //============================================ public members =============================================================

    /**
     * put in the "place" the actual date/time in the appropriate "format"
     * @param place
     * @param format
     */
    public void showActualTimeDate(long time){
        if (clockFace==null) return;
        if (clockFace.getVisibility()!=TextView.VISIBLE) return;
        clockFace.setText(Utils.milliSecondsToTimer(time));
    }
    
    public long getTimer() {
    	return refreshStep;
    }
    /**
     * start the ticking for all clocks
     * @param step the tick interval
     */
    public void start(long timer) { 
        refreshStep=timer;
        handler.removeCallbacks(mUpdateTimeTask);
        handler.postDelayed(mUpdateTimeTask, 0);
        isRuning = true;
        isFinish = false;
        if(clockFace.getVisibility() == View.INVISIBLE)
    		clockFace.setVisibility(View.VISIBLE);
        icon.setImageResource(R.drawable.btn_dong_ho_pressed);
    }
    /**
     * Stopping ticking all clocks (not removing them)
     * the calling could be put somewhere in onStop
     */
    public void stop() { 
    	isRuning = false;
    	isFinish = false;
    	icon.setImageResource(R.drawable.btn_dong_ho);
    	clockFace.setText(BiAnCungHoangDao.ctx.getResources().getString(R.string.hen_gio));
    	if(clockFace.getVisibility() == View.VISIBLE)
    		clockFace.setVisibility(View.INVISIBLE);
        handler.removeCallbacks(mUpdateTimeTask);
    }
    
    /**
     * timer finish
     */
    public void finish() {
    	isRuning = false;
    	isFinish = true;
    	icon.setImageResource(R.drawable.btn_dong_ho);
    	clockFace.setText(BiAnCungHoangDao.ctx.getResources().getString(R.string.hen_gio));
    	if(clockFace.getVisibility() == View.VISIBLE)
    		clockFace.setVisibility(View.INVISIBLE);
    	handler.removeCallbacks(mUpdateTimeTask);
    	BiAnCungHoangDao.ctx.getApplicationContext().stopService(PlayerView.playerService);
    	BiAnCungHoangDao.cancelNotification();
    	Clock.isFinish = false;
        BiAnCungHoangDao.ctx.finish();
    }
}